import Taro, { Component } from '@tarojs/taro'
import { View, Text } from '@tarojs/components'
import { DNavBar, DModal, DAgree, DAtInpute, DPaymodel } from '@/components'
import { AtForm, AtButton } from 'taro-ui'
import common from '@/common'
import './earnestform.scss'
import http from '@/api'
const getUrlScheme = http.getUrlScheme

// const itembg = require('../assets/people_right.png')
export default class earnestform extends Component {

  constructor(props) {
    super(props)
    this.state = {
      number: "",
      isOpened: false,
      totalmoney: "",
      money: "",
      list: [],
      statusCheck: false,
      userInfo: {
        creator: '',
        creatorContact: '',
        creatorId: '',
        entId: ''
      }
    }
  }
  componentWillMount() {
    let { id, qpid } = this.$router.params
    if (qpid) {
      this.setState({
        _obj: {
          refCarQuotedId: qpid,
          refCarSourceid: id,
          refCarSourceType: '1'
        }
      })
    } else {
      this.setState({
        _obj: {
          refCarSourceid: id,
          refCarSourceType: '2'
        }
      })
    }
    let _earnObj = JSON.parse(sessionStorage.getItem('_earnObj'))
    if (_earnObj) {
      let { creatorId = '', creatorContact = '', creator = '', entId = '', entName = '' } = _earnObj
      this.setState({
        userInfo: {
          ...this.state.userInfo,
          creatorId,
          creatorContact,
          creator,
          entId,
          entName
        }
      }, () => {
        sessionStorage.removeItem('_earnObj')
      })
    }
    this.getCarSourceDetail({ id })
    if (window.localStorage.getItem('ISPAY')) {
      // this.state.isOpened： = true
      this.setState({
        isOpened: true
      }, () => window.localStorage.removeItem('ISPAY'))
    }
  }
  //拉取信息
  getCarSourceDetail(params) {
    http.getCarSourceDetail(params).then(res => {
      this.setState({
        list: res
      })
    })
  }
  payFuc(type) {
    let act = 0
    this.setState({
      isOpened: false,
    })
    if (type) {
      act = 1
    }
    Taro.redirectTo({
      url: `/pages/shoporders/shoporders?active=${act}`
    })
  }
  //上传数据
  getEarnestForm(params) {
    http.getEarnestForm(params).then((res) => {
      setTimeout(async () => {
        if (params.payType == 8) {
          const urlScheme = await getUrlScheme(res.id, 1)
          window.location.href = urlScheme
          return
        }
        // Taro.navigateBack({
        //     delta: 2
        // })
        // Taro.redirectTo({
        //     url: `/pages/shoporders/shoporders?active=1`
        // })
        if (+params.payType === 1) {
          window.localStorage.setItem('ISPAY', 1)
        }
        if (!res.payUrl) {
          return Taro.showToast({
            title: '订单状态异常',
            icon: 'none'
          })
        }
        if (params.payType == 7) {
          let url = encodeURIComponent(res.payUrl)
          return Taro.navigateTo({
            url: `/pages/wxQrCode?payUrl=${url}`
          })
        }

        window.location.href = res.payUrl
      }, 1000)
    }).catch(() => {
      Taro.showToast({
        title: '网络异常!',
        icon: "none"
      })
    })
  }
  onChangeNumber(number) {
    this.setState({
      number
    })
  }
  onChangeTotalmoney(totalmoney) {
    let Digit = common.getDigit(totalmoney, this.state.totalmoney)
    this.setState({
      totalmoney: Digit
    })
  }

  onChangeMoney(money) {
    let Digit = common.getDigit(money, this.state.money)
    this.setState({
      money: Digit
    })
  }
  handleClose() {
    this.setState({
      ispayShow: false
    })
  }
  onCancelPay(payType) {
    this.state.paramsPay.payType = payType
    this.setState({
      ispayShow: false
    })
    this.getEarnestForm(this.state.paramsPay)
  }
  onClickPay() {
    let {
      number, totalmoney, money, statusCheck, userInfo, list, _obj
    } = this.state

    if (!number) {
      return Taro.showToast({
        title: '请输入成交数量',
        icon: 'none',
        duration: 800
      })
    } else if (!totalmoney) {
      return Taro.showToast({
        title: '请输入成交价格',
        icon: 'none',
        duration: 800
      })
    } else if (!money) {
      return Taro.showToast({
        title: '请输入订金金额',
        icon: 'none',
        duration: 800
      })
    } else if (Number(money) > Number(totalmoney) * 10000) {
      return Taro.showToast({
        title: '订金金额不可超过成交价格',
        icon: 'none',
        duration: 800
      })
    }
    else if (!statusCheck) {
      return Taro.showToast({
        title: '请阅读并同意《订金担保协议》',
        icon: 'none',
        duration: 800
      })
    }
    money = Number(money).toFixed(2)
    if (money < 1) {
      return Taro.showToast({
        title: '订金金额不能小于1元',
        icon: 'none',
        duration: 800
      })
    }
    totalmoney = Number(totalmoney).toFixed(2)
    let params = {
      payIp: window.payIp,
      payType: '1',
      "entDis": `${list.yentDis}`,
      "entId": `${list.entId}`,
      "entName": `${list.entName}`,
      "orCarEmissionStd": `${list.carEmissionStd}`,
      "orCarGuiPrice": `${list.carGuiPrice}`,
      "orCarInColor": `${list.carInColor}`,
      "orCarModel": `${list.carDataModel}`,
      "orCarModelId": `${list.carDataModelId}`,
      "orCarOutColor": `${list.carOutColor}`,
      "orCarSourceType": `${list.carSourceType}`,
      "orCount": `${number}`,
      "orEarnestPrice": `${money}`,
      "orHadPaidEarnestPrice": '1',
      "orIsReturnEarnestPrice": `1`,
      "orPrice": `${totalmoney}`,
      "orSale": `${userInfo.creator || list.creator}`,
      "orSaleComp": `${userInfo.entName || list.entName}`,
      "orSaleCompId": `${userInfo.entId || list.entId}`,
      "orSaleContact": `${userInfo.creatorContact || list.creatorContact}`,
      "orSaleId": `${userInfo.creatorId || list.creatorId}`,
      "orTotalPrice": `${totalmoney}`,
      ..._obj
    }
    // if (money > 3000) {
    //   return Taro.showToast({
    //     title: '支付金额超过3000元,请联系管理员线下支付'
    //   })
    // }
    this.setState({
      ispayShow: true,
      paramsPay: params
    })
    // this.getEarnestForm(params)
  }
  handleCheck(statusCheck) {
    this.setState({
      statusCheck
    })
  }
  render() {
    const {
      number,
      totalmoney,
      money,
      list,
      statusCheck,
      userInfo,
      ispayShow
    } = this.state
    return (
      <View className='b_earnestform'>
        <DNavBar title='发起订金担保'></DNavBar>
        <View className='b_earnestform__box'>
          <AtForm>
            <View className="b_earnestform__info">
              <View className="item_title">
                <Text className="item_title__left">卖家名称</Text>
                <Text className="item_title__right">{userInfo.entName || list.entName}</Text>
              </View>
              <View className="item_title">
                <Text className="item_title__left">卖家联系人</Text>
                <Text className="item_title__right">{userInfo.creator || list.creator}</Text>
              </View>
              <View className="item_title">
                <Text className="item_title__left">卖家联系方式</Text>
                <Text className="item_title__right">{userInfo.creatorContact || list.creatorContact}</Text>
              </View>
            </View>
            <View className="b_earnestform__info">
              <Text className='b_earnestform__info_title'>车辆信息</Text>
              <View className="item_title">
                <Text className="item_title__left">交易车型</Text>
                <Text className="item_title__right">{list.carDataModel}</Text>
              </View>
              <View className="item_title">
                <Text className="item_title__left">车源类型</Text>
                <Text className="item_title__right">{list.carSourceType}</Text>
              </View>
              <View className="item_title">
                <Text className="item_title__left">车辆指导价</Text>
                <Text className="item_title__right">{window.toFixed(list.carGuiPrice)}万元</Text>
              </View>
              <View className="item_title">
                <Text className="item_title__left">外观颜色</Text>
                <Text className="item_title__right">{list.carOutColor}</Text>
              </View>
              <View className="item_title">
                <Text className="item_title__left">内饰颜色</Text>
                <Text className="item_title__right">{list.carInColor}</Text>
              </View>
              <View className="item_title">
                <Text className="item_title__left">排放标准</Text>
                <Text className="item_title__right">{list.carEmissionStd}</Text>
              </View>
            </View>
            <View className="b_earnestform__info b_earnestform__transaction">
              <Text className='b_earnestform__info_title'>交易信息</Text>
              <DAtInpute
                value={number}
                onChange={this.onChangeNumber.bind(this)}

                title='成交数量'
                type="number"

                require
                placeholder='请输入成交数量'>
                <Text>台</Text>
              </DAtInpute>
              <DAtInpute
                value={totalmoney}
                require
                onChange={this.onChangeTotalmoney.bind(this)}
                type="number"
                title='成交总价'
                placeholder='请输入成交价格'>
                <Text>万元</Text>
              </DAtInpute>
              <DAtInpute
                value={money}
                onChange={this.onChangeMoney.bind(this)}
                type="number"
                title='订金金额'
                require
                maxLength={8}
                placeholder='请输入您需支付订金金额'>
                <Text>元</Text>
              </DAtInpute>
            </View>
            <View className='b_earnestform__foot'>
              <DAgree type='1' value={statusCheck} text={'《订金担保协议》'} onHandleChange={this.handleCheck.bind(this)} className='b_earnestform__foot_agree'></DAgree>
              <View className='b_earnestform__footer'>
                <View className="b_earnestform__footer_text">
                  <Text className="item_title__left">需支付订金:</Text>
                  <Text className="item_title__right">￥{money || 'xxxx'}</Text>
                </View>
                <AtButton className='item_btn' onClick={this.onClickPay.bind(this)}>支付订金</AtButton>
              </View>
            </View>
            {/* </View> */}
          </AtForm>
          <DModal isShow={this.state.isOpened} title="支付提示" text="是否已完成支付？"
            firstbtn="支付失败" lastbtn="支付完成" onFirst={this.payFuc.bind(this)} onCancel={this.payFuc.bind(this)} onLast={this.payFuc.bind(this, 1)}></DModal>
          <DPaymodel handleClose={this.handleClose.bind(this)} onCancel={this.onCancelPay.bind(this)} isShow={ispayShow} />
        </View>
      </View >
    )
  }
}
